﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;


namespace firmarPDF
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            mostrar();
        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            if (FileUploadControl.HasFile)
            {
                try
                {
                    string filename = Path.GetFileName(FileUploadControl.FileName);
                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                    StatusLabel.Text = "Upload status: File uploaded!";
                    
                    // Archivo subido, añadimos la firma
                    /*
                    string path = Server.MapPath("PDFs");
                    Document doc1 = new Document();
                    PdfWriter.GetInstance(doc1, new FileStream(Server.MapPath("~/") + filename + "2", FileMode.Create));

                    doc1.Open();
                    doc1.Add(new Paragraph("My first PDF"));
                    doc1.Close();
                    label1.Text = "<a href='" + Server.MapPath("~/") + filename + "'>Enlace </a>";
                     * */
                    X509Store objStoreUsuario = new X509Store(StoreName.My, StoreLocation.LocalMachine);

                    objStoreUsuario.Open(OpenFlags.ReadOnly);

                    foreach (X509Certificate2 objCert in objStoreUsuario.Certificates)
                    {
                        CFirmaPDFcs.SignHashed(Server.MapPath("~/") + filename, Server.MapPath("~/") + "dondesea.pdf", objCert, "razon","Location", true);
                    }
                    StatusLabel.Text += "<a href='" +Server.MapPath("~/") + filename+"'>Enlace </a>";
                    iframepdf.Src = filename;

                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
        }

        public String mostrar()
        {

            // De usuario
            X509Store objStoreUsuario = new X509Store(StoreName.My, StoreLocation.LocalMachine);

            objStoreUsuario.Open(OpenFlags.ReadOnly);

            foreach (X509Certificate2 objCert in objStoreUsuario.Certificates)
            {
                listaCertificadosUsuario.Items.Add(objCert.SubjectName.Name + ": " + objCert.Thumbprint);
                
                StringBuilder objSB = new StringBuilder("Detalle del certificado: \n\n");
                //Detalle
                objSB.AppendLine("Persona: " + objCert.Subject);
                objSB.AppendLine("Emisor: " + objCert.Issuer);
                objSB.AppendLine("Válido desde: " + objCert.NotBefore.ToString());
                objSB.AppendLine("Válido hasta: " + objCert.NotAfter.ToString());
                objSB.AppendLine("Tamaño de la clave: " + objCert.PublicKey.Key.KeySize.ToString());
                objSB.AppendLine("Número de serie: " + objCert.SerialNumber);
                objSB.AppendLine("Hash: " + objCert.Thumbprint);
                label1.Text = objSB.ToString();

            }

            objStoreUsuario.Close();

            // Del servidor
            X509Store objStoreServidor = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            objStoreServidor.Open(OpenFlags.ReadOnly);

            foreach (X509Certificate2 objCert in objStoreServidor.Certificates)
            {
                listaCertificadosServidor.Items.Add(objCert.SubjectName.Name + ": " + objCert.Thumbprint);
                StringBuilder objSB = new StringBuilder("Detalle del certificado: \n\n");
                //Detalle
                objSB.AppendLine("Persona: " + objCert.Subject);
                objSB.AppendLine("Emisor: " + objCert.Issuer);
                objSB.AppendLine("Válido desde: " + objCert.NotBefore.ToString());
                objSB.AppendLine("Válido hasta: " + objCert.NotAfter.ToString());
                objSB.AppendLine("Tamaño de la clave: " + objCert.PublicKey.Key.KeySize.ToString());
                objSB.AppendLine("Número de serie: " + objCert.SerialNumber);
                objSB.AppendLine("Hash: " + objCert.Thumbprint);
                label2.Text = objSB.ToString();

            }

            objStoreServidor.Close();


            return "";
        }
    }



}